package org.socialresume.client.mvp.view.widget;

import java.util.List;

import org.socialresume.client.model.Feed;
import org.socialresume.client.model.FeedItem;
import org.socialresume.client.service.GreetingService;
import org.socialresume.client.service.GreetingServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FeedWidget extends Composite implements HasSelectionHandlers<Feed>{
	protected final GreetingServiceAsync updateService = GWT.create(GreetingService.class);
	
	protected AsyncCallback<List<FeedItem>> onUpdateCallback =  new AsyncCallback<List<FeedItem>>() {

		@Override
		public void onFailure(Throwable caught) {
			hideLoadingWidget();
			GWT.log("Erreur pendant la recuperation des updates", caught);
		}

		@Override
		public void onSuccess(List<FeedItem> result) {
			feed.setUpdates(result);
			hideLoadingWidget();
		}
	};
	
	private Anchor label;
	protected Feed feed;
	private Widget loadingWidget;

	public FeedWidget(Feed aFeed) {
		this.feed = aFeed;
		VerticalPanel body = new VerticalPanel();
		body.add(new HTML(feed.getDescription()));
		this.label = new Anchor(feed.getTitle(),feed.getUrl());
		this.label.setWordWrap(false);
		this.label.setStyleName("link");
		body.add(this.label);
		FocusPanel container = new FocusPanel();
		HorizontalPanel horizontalContainer = new HorizontalPanel();
		horizontalContainer.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		Image logo = new Image(feed.getLogoUrl());
		logo.setVisibleRect(0, 0, 70, 70);
		
		final Label seeUpdateLabel = new Label(">>");
		seeUpdateLabel.setVisible(false);
		seeUpdateLabel.setTitle("Click to see updates");
		seeUpdateLabel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				SelectionEvent.fire(FeedWidget.this,feed);
			}
		});

		horizontalContainer.add(logo);//
		horizontalContainer.add(body);
		horizontalContainer.add(seeUpdateLabel);
		
		horizontalContainer.setCellWidth(logo, 			"70px");
		horizontalContainer.setCellWidth(body, 		"210px");
		horizontalContainer.setCellWidth(seeUpdateLabel, 	"20px");

		logo.setStyleName("feedLogo");
		seeUpdateLabel.setStyleName("seeUpdatesButton");
		
		container.setWidget(horizontalContainer);
		container.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				seeUpdateLabel.setVisible(true);
			}
		});
		
		container.addMouseOutHandler(new MouseOutHandler() {
			
			@Override
			public void onMouseOut(MouseOutEvent event) {
				seeUpdateLabel.setVisible(false);
			}
		});
		initWidget(container);
	}
	
	public final void update(){
		showLoadingWidget();
		doUpdate();
	}
	
	protected void doUpdate() {
		updateService.getUpdates(feed.getType(),feed.getUrl(),onUpdateCallback);
	}
	
	@Override
	public HandlerRegistration addSelectionHandler(
			SelectionHandler<Feed> handler) {
		return addHandler(handler, SelectionEvent.getType());
	}

	public Feed getFeed() {
		return feed;
	}

	public void setLoadingWidget(Widget loadingWidget) {
		this.loadingWidget = loadingWidget;
	}
	
	private void showLoadingWidget(){
		if(loadingWidget!=null){
			loadingWidget.setVisible(true);
		}
	}
	

	private void hideLoadingWidget(){
		if(loadingWidget!=null){
			loadingWidget.setVisible(false);
		}
	}
}
